//
//  MixedPanelGroupTableViewCell.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 17/1/6.
//  Copyright © 2017年 com.xtion.crm. All rights reserved.
//

#import "MixedPanelGroupTableViewCell.h"
#import "MixedPanel.h"
#import "XHSoundRecorder.h"
#import "ShowVideoViewController.h"

@interface MixedPanelGroupTableViewCell ()<MixedPanelDelegate>
{
    MixedPanel *_mixedPanel;
    
    NSMutableArray *_selectedImagePath;
}
@end

@implementation MixedPanelGroupTableViewCell

@synthesize selectedImagePath = _selectedImagePath;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    _selectAddress = @"";
    _selectedImagePath = [NSMutableArray new];
    _selectedFilePath = [NSMutableArray new];
    _selectedVoicePath = [NSMutableArray new];
    _selectedRecordPath = [NSMutableArray new];
    
    _mixedPanel = [[MixedPanel alloc]initWithFrame:CGRectMake(0, 5, self.width, 70) mixedPanelGroupTableViewCell:self];
    _mixedPanel.mixedDelegate = self;
    [self.contentView addSubview:_mixedPanel];
    
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    _mixedPanel.size = CGSizeMake(self.width, 70);
}

-(void)addImage:(UIImage *)image withTag:(NSString *)tag
{
    [_selectedImagePath addObject:tag];
    
    [_mixedPanel addImage:image withTag:tag];
}

-(void)replaceImage:(UIImage*)image atTag:(NSString*)tag
{
    [_mixedPanel replaceImage:image withTag:tag];
}

-(void)clickAddImageButtonOfMixedPanel:(MixedPanel*)imagePanel
{
    [imagePanel show];
}

-(void)clickDeleteImageButtonOfMixedPanel:(MixedPanel*)imagePanel
{
    
}

-(void)clickDeleteModeImageButtonOfMixedPanel:(MixedPanel*)imagePanel atTag:(NSString*)tag
{
    [_selectedImagePath removeObject:tag];
    
    [imagePanel deleteImageWithTag:tag];
}

-(void)mixedPanel:(MixedPanel*)imagePanel didClickedImageButton:(MixedImageButton*)imageButton imageContains:(NSArray*)imageContains
{
    NSString *imagePath = imageButton.szTag;
    if([XMSFileHelper isFileExist:imagePath])
    {
        if([[imagePath pathExtension] isEqualToString:@"wav"])
        {
            [self playsound:imagePath withFinishPlaying:nil];
        }
        else if([[imagePath pathExtension] isEqualToString:@"mov"])
        {
            [self showVideo:imagePath];
        }
        else
        {
            NSMutableArray *arThumb = [NSMutableArray new];
            NSMutableArray *arOriURL = [NSMutableArray new];
            NSMutableArray *arContain = [NSMutableArray new];
            for (NSInteger i = 0; i < imageContains.count; i ++)
            {
                MixedImageButton *btn = imageContains[i];
                if([btn.szTag isEqualToString:imageButton.szTag])
                {
                    [arContain addObject:btn];
                    break;
                }
            }
            
            [arThumb addObject:imagePath];
            [arOriURL addObject:imagePath];
            ImageBrowserViewController *imgVcl = [[ImageBrowserViewController alloc]initWithThumbImagePath:arThumb originImageURL:arOriURL downloadSavePath:[XMSFileHelper tempPath]];
            [imgVcl defaultSelectAtIndex:0];
            [imgVcl zoomPresentPreviewImageViewController:arContain];
        }
    }
}

//播放录音
- (void)playsound:(NSString *)filePath withFinishPlaying:(void (^)())FinishPlaying {
    [[XHSoundRecorder sharedSoundRecorder] playsound:filePath withFinishPlaying:nil];
}

//播放视频
-(void)showVideo:(NSString*)videoPath
{
    XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
    p.param[ShowVideoViewControllerVideoAbsolutePathParamKey] = videoPath;
    
    [XUINavigationController pushViewControllerClass:[ShowVideoViewController class] withSwitchParam:p];
}

+ (CGFloat)caleHeight
{
    return 80;
}
@end

@interface MixedPanelStaticGroupTableViewCell ()<MixedPanelDelegate>
{
    MixedPanel *_imagePanel;
    
    UILabel *_titleLabel;
    
    NSMutableArray *_imageFiles;
}
@end

@implementation MixedPanelStaticGroupTableViewCell

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    _imageFiles = [NSMutableArray new];
    
    _imagePanel = [[MixedPanel alloc]initWithFrame:CGRectMake(0, 5, self.width, 70)];
    _imagePanel.mixedDelegate = self;
    [self.contentView addSubview:_imagePanel];
    
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    if(_titleLabel != nil)
    {
        _titleLabel.left = 10;
        _titleLabel.centerY = self.contentView.height / 2;
        
        _imagePanel.left = _titleLabel.right + 10;
        _imagePanel.size = CGSizeMake(self.contentView.width - _titleLabel.right + 10, 70);
    }
    else
    {
        _imagePanel.size = CGSizeMake(self.contentView.width, 70);
    }
}

-(void)setTitle:(NSString *)title
{
    if(title.length > 0)
    {
        if(_titleLabel == nil)
        {
            CGSize size = [NSString charSizeWithSystemFontSize:13];
            _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, size.width * 6, size.height)];
            _titleLabel.backgroundColor = [UIColor clearColor];
            _titleLabel.textColor = [UIColor grayColor];
            _titleLabel.font = [UIFont systemFontOfSize:13];
            _titleLabel.textAlignment = NSTextAlignmentRight;
            [self.contentView addSubview:_titleLabel];
        }
        _titleLabel.text = title;
    }
    else
    {
        [_titleLabel removeFromSuperview];
        _titleLabel = nil;
    }
}

-(void)addImage:(UIImage *)image withTag:(NSString *)tag
{
    [_imageFiles addObject:tag];
    
    [_imagePanel addImage:image withTag:tag];
}

-(void)replaceImage:(UIImage*)image atTag:(NSString*)tag
{
    [_imagePanel replaceImage:image withTag:tag];
}

-(void)mixedPanel:(MixedPanel *)imagePanel didClickedImageButton:(MixedImageButton *)imageButton imageContains:(NSArray *)imageContains
{
    [(id<MixedPanelStaticGroupTableViewCelldelegate>)self.delegate mixedPanelStaticGroupTableViewCell:self clickedImageButtonTag:imageButton.szTag imageContain:imageContains];
}

+ (CGFloat)caleHeight
{
    return 80;
}

-(void)setImageNames:(NSArray *)imageNames
{
    for (NSString *item in imageNames)
    {
        UIImage *image = [UIImage imageWithContentsOfFile:item];
        [self addImage:image withTag:item];
    }
}

-(NSArray*)imageNames
{
    return _imageFiles;
}

@end
